package com.atwisdom.star.service.equipPoint.impl;

import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.util.JsonUtil;
import com.atwisdom.star.common.vo.JsonResult;
import com.atwisdom.star.common.vo.ModelType;
import com.atwisdom.star.common.vo.ResultInfo;
import com.atwisdom.star.core.bsl.modelClass.resource.Impl.ResourceInstanceBslServiceImpl;
import com.atwisdom.star.core.dao.pojo.modelClass.hier.HierInstance;
import com.atwisdom.star.core.dao.pojo.modelClass.logic.LogicInstance;
import com.atwisdom.star.core.dsl.modelClass.hier.Impl.HierInstanceDslServiceImpl;
import com.atwisdom.star.core.dsl.modelClass.logic.Impl.LogicInstanceDslServiceImpl;
import com.atwisdom.star.core.dsl.modelClass.resource.Impl.ResourceInstanceDslServiceImpl;
import com.atwisdom.star.exception.pojo.BusinessException;
import com.atwisdom.star.service.equipPoint.EquipPointService;
import com.atwisdom.star.service.util.LogicUtil;
import com.atwisdom.star.service.util.ResourceUtil;
import com.atwisdom.star.service.util.ValidateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @ClassName: EquipPointServiceImpl
 * @ Description:
 * @author: wjj
 * @Date 2024/7/17
 */
@Service
public class EquipPointServiceImpl implements EquipPointService {

    @Autowired
    private LogicUtil logicUtil;

    @Autowired
    private HierInstanceDslServiceImpl hierInstanceDslServiceImpl;

    @Autowired
    private LogicInstanceDslServiceImpl logicInstanceDslServiceImpl;

    @Autowired
    ResourceInstanceBslServiceImpl resourceInstanceBslServiceImpl;

    @Autowired
    private ResourceInstanceDslServiceImpl resourceInstanceDslServiceImpl;

    @Autowired
    ResourceUtil resourceUtil;

    private final String CLASS_NAME_EQUIPMENT = "EquipPoint";
    private final String TYPE = "logic";
    private final String equipPointModel = "logic";
    private final String equipPointLinkType = "resource";
    private final String equipPointLinkClassName = "collectionPoint";
    private final String equipPointClassName = "EquipPoint";
    private final String equipPointParentId = "2g9jnxxjfg7400";


    /**
     * 根据设备id查询logic容器存在返回不存在新增
     *
     * @param data
     * @return 设备容器及其hier
     * @throws BusinessException
     */
    @Override
    @Transactional
    public ResultInfo<?> queryEquipPointByEquId(JSONObject data) throws BusinessException {
        int id = data.getIntValue("id");
        if (null == data.get("id")) {
            return JsonResult.error(500, "id不能为空");
        }
        JSONObject values = JsonUtil.buildJson(new Object[]{"if_equ", id, "sf_name", data.getString("sf_name"), "sf_code", data.getString("sf_code"), "bf_template", true});
        return JsonResult.success(logicUtil.queryUpdateOrAdd(CLASS_NAME_EQUIPMENT, TYPE, values, "if_equ", "bf_template"));
    }

    /**
     * 该设备容器下是否有采集点实体
     *
     * @param data
     * @return 是否有采集点实体
     * @throws BusinessException
     */
    @Override
    @Transactional
    public ResultInfo<?> isEquipHasContent(JSONObject data) throws BusinessException {
        int id = data.getIntValue("id");
        if (null == data.get("id")) {
            return JsonResult.error(500, "id不能为空");
        }
        String condition = "className=='" + CLASS_NAME_EQUIPMENT + "'&&type=='" + TYPE + "'&&if_equ==" + id + "&&bf_template==true";
        List<LogicInstance> list = logicInstanceDslServiceImpl.fetchInstanceList(condition, CLASS_NAME_EQUIPMENT, null);
        if (list.size() > 0) {  //查hier
            LogicInstance l = list.get(0);
            List<HierInstance> hierInstances = this.hierInstanceDslServiceImpl.queryByContainerId(ModelType.Logic.getCode(), l.getClassName(), l.getId());

            if (hierInstances.size() > 2) {
                return JsonResult.success(false);
            }
        }
        return JsonResult.success(true);
    }

    /**
     * 新增采集点并关联设备容器
     *
     * @param data
     * @return 采集点实例
     * @throws BusinessException
     */
    @Override
    @Transactional
    public ResultInfo<?> addCollectionPoint(JSONObject data) throws BusinessException {
        String type = data.getString("type");
        String className = data.getString("className");
        String name = data.getString("name");
        JSONObject values = data.getJSONObject("values");

        String containerId = data.getString("containerId");
        ValidateUtil.hasEmpty(containerId, type, className, name, values);
        // 添加资源实例
        JSONObject addJson = resourceInstanceDslServiceImpl.add(type, className, name, values);
        if (addJson != null && StringUtils.isNotEmpty(addJson.getString("id"))) {
            String linkId = addJson.getString("id");
            JSONObject hierInsJson = JsonUtil.buildJson(new Object[]{"containerModel", this.equipPointModel, "containerClassName", this.equipPointClassName, "containerId", containerId, "parentId", this.equipPointParentId, "linkId", linkId, "linkType", equipPointLinkType, "linkClassName", equipPointLinkClassName, "values", JsonUtil.get(addJson, "values")});
            hierInstanceDslServiceImpl.addChild(hierInsJson);
        }
        return JsonResult.success(addJson);
    }

    /**
     * 已有采集点关联设备容器-未启用
     */
    @Override
    @Transactional
    public ResultInfo<?> linkCollectionPoint(JSONObject data) throws BusinessException {
        ValidateUtil.hasEmpty(data.get("containerId"), data.get("linkId"), data.get("linkType"), data.get("linkClassName"));
        JSONObject hierInsJson = JsonUtil.buildJson(new Object[]{"containerModel", this.equipPointModel, "containerClassName", this.equipPointClassName, "containerId", JsonUtil.get(data, "containerId"), "parentId", this.equipPointParentId, "linkId", JsonUtil.get(data, "linkId"), "linkType", JsonUtil.get(data, "linkType"), "linkClassName", JsonUtil.get(data, "linkClassName"), "values", ""});
        hierInstanceDslServiceImpl.addChild(hierInsJson);
        return JsonResult.success(data);
    }
}